<?php

namespace app\admin\controller;

use app\lib\util\excel;
use think\facade\Db;
use think\facade\View;

class Cloud extends Common
{
    public function orderList(){
        $com = $this->pidTokenId();
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();

        //查询
        $data = request()->get();

        if (!empty($data)){
            if(!empty($data['trade_channel'])){
                $where['trade_channel'] = $data['trade_channel'];//电话
            }
            if(!empty($data["start_time"])&&(empty($data["end_time"]))){
                $start_time=strtotime($data["start_time"])*1000;
                $where[] = ['gmt_payment','>=',$start_time];
            }else if(empty($data["start_time"])&&(!empty($data["end_time"]))){
                $end_time=strtotime($data["end_time"])*1000;
                $where[] = ['gmt_payment','<=',$end_time];
            }else if(!empty($data["start_time"])&&(!empty($data["end_time"]))){
                $start_time=strtotime($data["start_time"])*1000;
                $end_time=strtotime($data["end_time"])*1000;
                $where[] = ['gmt_payment','>=',$start_time];
                $where[] = ['gmt_payment','<=',$end_time];
            }
            View::assign('where',$where);
            $where = array_filter($where);
        }
        $export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        View::assign('export',$export);

        $store_id=Db::table("cloud_store")
            ->where("community_id",'in',$com["community_id"])
            ->column("store_id");
        //var_dump($store_id);exit;
        $storeList=Db::table("cloud_store")
            ->whereIn("store_id",$store_id)
            ->select();
        $storeMap=[];
        foreach ($storeList as $key=>$value){
            $storeMap[$value['store_id']]=$value["store_name"];
        }

        $list=Db::table("cloud_order")
            ->whereIn("store_id",$store_id)
            ->where($where)
            ->order('id desc')->paginate( [
                'list_rows'=> 50,
                'page'  =>  $page,
                'query' =>  $where
            ]);

        $number=Db::table("cloud_order")->whereIn("store_id",$store_id)->where($where)->count();


        $orderlist=array();
        foreach ($list as $key=> $info){
            if(isset($storeMap[$info["store_id"]])){
                $info["store_name"] =$storeMap[$info["store_id"]];
            }else{
                $info["store_name"]=$info["store_id"];
            }
//            $store=Db::table("cloud_store")->where("store_id",$info["store_id"])
//                ->where("is_delete",0)->find();
//            if(empty($store)){
//                $info["store_name"]=$info["store_id"];
//            }else{
//                $info["store_name"]=$store["store_name"];
//            }


            $orderlist[]=$info;
        }
        $pageRender=$list->render();
        View::assign('pageRender',$pageRender);
        View::assign('list',$orderlist);

        View::assign('number',$number);

        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch("order_list");
    }

    public function exportExcel(){
        $com = $this->pidTokenId();
        $export=request()->get("export");
        $export = str_replace("|","\"",$export);
        $where = json_decode($export,true);

        $store_id=Db::table("cloud_store")
            ->whereIn("community_id",$com["community_id"])
            ->column("store_id");
        $storeList=Db::table("cloud_store")
            ->whereIn("store_id",$store_id)
            ->select();
        $storeMap=[];
        foreach ($storeList as $key=>$value){
            $storeMap[$value['store_id']]=$value["store_name"];
        }
        $list = Db::table("cloud_order")
            ->whereIn("store_id",$store_id)
            ->where($where)
            ->limit(5000)
            ->select();

        $fileName="账单表";
        $title="账单表";
        $headArr = ['小区名', '金额', '付款方式','备注','付款时间'];
        $data=[];
        foreach ($list as $key=> $info){
            $value=[];
            //$data=$info
            if(isset($storeMap[$info["store_id"]])){
                $value["store_name"] =$storeMap[$info["store_id"]];
            }else{
                $value["store_name"]=$info["store_id"];
            }
            $value["receipt_amount"]=$info["receipt_amount"];
            if($info["trade_channel"]=="alipay"){
                $value["trade_channel"]="支付宝";
            }else if($info["trade_channel"]=="wechat"){
                $value["trade_channel"]="微信";

            }
            $value["remarks"]=$info["remarks"];
            $value["gmt_payment"]=date('Y-m-d H:i:s',$info["gmt_payment"]/1000);
            $data[]=$value;
        }
        //var_dump($data);exit;
//        $data = [
//            [1,'老刘','男'],
//            [2,'老马','女'],
//            [3,'小刘','男'],
//        ];
        $excelObj = new excel();
        $excelObj->excelExport($title, $headArr, $data,$fileName);
    }
}